/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBHandling;

import Entity.Category;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author HINSKY
 */
public class CategoryHandling {

    private DBUtil du;

    public CategoryHandling() {
        du = new DBUtil();
    }

    public Vector<Category> getCategories() throws Exception {
        ResultSet rs = du.executeSQL("Select * from Category");   //get all items in database

        Vector<Category> categories = new Vector<Category>();
        while (rs.next()) {
            Category cate = new Category();
            cate.setCateID(rs.getString("categoryID"));
            cate.setCateName(rs.getString("categoryName"));

            categories.add(cate);
        }
        du.closeConnection();
        return categories;
    }

    public boolean insertCate(Category cate) throws Exception {
        try {
            String sql = "{call sp_addCate" + "(?, ?)}";
            Object[] objects = {cate.getCateID(), cate.getCateName()};
            return du.executeProcedureUpdate(sql, objects);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public boolean updateCate(Category cate, String id) throws Exception {
        try {
            String sql = "{call sp_updateCate" + "(?, ?, ?)}";
            Object[] objects = {id, cate.getCateID(), cate.getCateName()};

            return du.executeProcedureUpdate(sql, objects);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public boolean removeCate(String id) throws Exception {
        try {
            String sql = "{call sp_removeCate" + "(?)}";
            Object[] objects = {id};

            return du.executeProcedureUpdate(sql, objects);
        } catch (Exception ex) {
            throw ex;
        }
    }
}
